<template>
    <div class="wrap df jsc aic">
        <div class="loginBox df">
            <div class="left df jsc aic">
                <h1>源码时代</h1>
            </div>
            <div class="right">
                <h2>欢迎登录</h2>
                <div style="width:100px">{{ token }}</div>
                <el-form :rules="rules" :model="formData" ref="loginForm">
                    <el-form-item prop="account">
                        <el-input v-model="formData.account">
                            <template #prefix>
                                <el-icon class="el-input__icon">
                                    <svg-icon name="account" fill="#ccc" size="mini"></svg-icon>
                                </el-icon>
                            </template>
                        </el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input v-model="formData.password" type="password" show-password>
                            <template #prefix>
                                <el-icon class="el-input__icon">
                                    <svg-icon name="data" fill="#ccc" size="mini"></svg-icon>
                                </el-icon>
                            </template>
                        </el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="loginSubmit">登录</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue';
import type { FormInstance, FormRules } from 'element-plus'
import { useUserStore } from "@/store/login"
import { storeToRefs } from 'pinia';
import { useRouter, useRoute } from 'vue-router';


//表单数据
let formData = reactive({
    account: "",
    password: ""
})

// 表单规则
const rules = reactive<FormRules>({
    account: [
        { required: true, message: '请输入账号', trigger: 'blur' },
        { min: 3, max: 10, message: '长度必须在3-10之间', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 3, max: 10, message: '长度必须在3-10之间', trigger: 'blur' },
    ]
})

let loginForm = ref<FormInstance>()
let userStore = useUserStore()
let { token, role } = storeToRefs(userStore)
let $router = useRouter()
// 登录请求
let loginSubmit = () => {
    loginForm.value!.validate(async (valid, fields) => {
        if (valid) {
            console.log("登录");
            console.log(formData);
            await userStore.store_login(formData)
            // 跳转
            $router.push("/home")
        } else {
            console.log("验证不通过");
        }
    })
}



</script>

<style scoped lang="scss">
.wrap {
    background-color: #DCDFE6;
    height: 100%;


    .loginBox {
        height: 300px;
        background-color: white;
        padding: 20px 30px;

        .right {
            flex: 1;


            .el-button {
                width: 100%;
            }
        }
    }
}
</style>